package com.ruoyi.web.controller.module.association;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.association.domain.CustomerCollectDynamic;
import com.ruoyi.association.domain.CustomerCollectEmployee;
import com.ruoyi.association.service.ICustomerCollectDynamicService;
import com.ruoyi.association.service.ICustomerCollectEmployeeService;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.dynamic.domain.Dynamic;
import com.ruoyi.dynamic.service.IDynamicService;
import com.ruoyi.employee.domain.EmployeeUser;
import com.ruoyi.customer.domain.CustomerUser;
import com.ruoyi.employee.service.IEmployeeUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 关联信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/association")
public class AssociationController extends BaseController
{
    @Autowired
    private ICustomerCollectEmployeeService customerCollectEmployeeService;

    @Autowired
    private IEmployeeUserService employeeUserService;

    @Autowired
    private ICustomerCollectDynamicService customerCollectDynamicService;

    @Autowired
    private IDynamicService dynamicService;

    /**
     * 客户收藏店员 - 保存
     */
    @PostMapping("/collectEmployeeSave")
    public AjaxResult collectEmployeeSave(@Validated @RequestBody CustomerCollectEmployee customerCollectEmployee)
    {
        Long userId = SecurityUtils.getUserId();
        customerCollectEmployee.setCustomerId(userId);
        String key = CacheConstants.CUSTOMER_COLLECT_EMPLOYEE + userId.toString() + "_" + customerCollectEmployee.getEmployeeId().toString();
        JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject((key));
        if (StringUtils.isNotNull(arrayCache))
        {
            // 已存在，删除
            SpringUtils.getBean(RedisCache.class).deleteObject(key);
            return toAjax(customerCollectEmployeeService.deleteCustomerEmployeeById(userId, customerCollectEmployee.getEmployeeId()));
        }else{
            // 不存在，新增一条记录
            List<CustomerCollectEmployee> list = new ArrayList<>();
            for(int x = 0; x < 1; x++){
                CustomerCollectEmployee ce = new CustomerCollectEmployee();
                ce.setEmployeeId(customerCollectEmployee.getEmployeeId());
                ce.setCustomerId(userId);
                list.add(ce);
            }
            SpringUtils.getBean(RedisCache.class).setCacheObject(key, list);
            return toAjax(customerCollectEmployeeService.batchCustomerEmployee(list));
        }
    }

    /**
     * 客户收藏店员 - 列表
     */
    @PostMapping("/collectEmployeeList")
    public TableDataInfo collectEmployeeList(@RequestBody CustomerCollectEmployee customerCollectEmployee)
    {
        startPageByPost();
        List<CustomerCollectEmployee> list = customerCollectEmployeeService.selectCustomerCollectEmployeeList(customerCollectEmployee);
        for(CustomerCollectEmployee eu : list){
            if (StringUtils.isNotNull(eu.getCustomerId())){
                EmployeeUser employeeUser = employeeUserService.selectUserById(eu.getEmployeeId());
                if(StringUtils.isNotNull(employeeUser)){
                    eu.setEmployeeUser(employeeUser);
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 客户收藏店员 店员发布的动态 - 列表
     */
    @PostMapping("/collectEmployeeDynamicList")
    public TableDataInfo collectEmployeeDynamicList(@RequestBody CustomerCollectEmployee customerCollectEmployee)
    {
        startPageByPost();
        List<Dynamic> list = customerCollectEmployeeService.selectCustomerCollectEmployeeDynamicList(customerCollectEmployee.getCustomerId());
        Long userId = SecurityUtils.getUserId();
        for(Dynamic dy : list){
            String key = CacheConstants.CUSTOMER_COLLECT_DYNAMIC + userId.toString() + "_" + dy.getDynamicId().toString();
            JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject((key));
            if (StringUtils.isNotNull(arrayCache))
            {
                dy.setIsLike(UserConstants.YES);
            }else{
                dy.setIsLike(UserConstants.NO);
            }
        }
        return getDataTable(list);
    }


    /**
     * 客户收藏动态 - 保存
     */
    @PostMapping("/collectDynamicSave")
    public AjaxResult collectDynamicSave(@Validated @RequestBody CustomerCollectDynamic customerCollectDynamic)
    {
        Long userId = SecurityUtils.getUserId();
        customerCollectDynamic.setCustomerId(userId);
        String key = CacheConstants.CUSTOMER_COLLECT_DYNAMIC + userId.toString() + "_" + customerCollectDynamic.getDynamicId().toString();
        JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject((key));
        if (StringUtils.isNotNull(arrayCache))
        {
            // 已存在，删除
            SpringUtils.getBean(RedisCache.class).deleteObject(key);
            dynamicService.updateDynamicNumLikeReduce(customerCollectDynamic.getDynamicId());
            return toAjax(customerCollectDynamicService.deleteCustomerDynamicById(userId, customerCollectDynamic.getDynamicId()));
        }else{
            // 不存在，新增一条记录
            List<CustomerCollectDynamic> list = new ArrayList<>();
            for(int x = 0; x < 1; x++){
                CustomerCollectDynamic ce = new CustomerCollectDynamic();
                ce.setDynamicId(customerCollectDynamic.getDynamicId());
                ce.setCustomerId(userId);
                list.add(ce);
            }
            SpringUtils.getBean(RedisCache.class).setCacheObject(key, list);
            dynamicService.updateDynamicNumLikeAdd(customerCollectDynamic.getDynamicId());
            return toAjax(customerCollectDynamicService.batchCustomerDynamic(list));
        }
    }

    /**
     * 客户收藏动态 - 列表
     */
    @PostMapping("/collectDynamicList")
    public TableDataInfo collectDynamicList(@RequestBody CustomerCollectDynamic customerCollectDynamic)
    {
        startPageByPost();
        List<CustomerCollectDynamic> list = customerCollectDynamicService.selectCustomerCollectDynamicList(customerCollectDynamic);
        for(CustomerCollectDynamic cd : list){
            if (StringUtils.isNotNull(cd.getCustomerId())){
                Dynamic dynamic = dynamicService.selectDynamicById(cd.getDynamicId());
                if(StringUtils.isNotNull(dynamic)){
                    cd.setDynamic(dynamic);
                }
            }
        }
        return getDataTable(list);
    }

}
